<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
    }

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		redirect('admin_panel/client/main');
	}
    
    public function main()
    {
        //init
        $crud = new grocery_CRUD();
        $crud->set_table('clients');
        $crud->set_subject('Clients');
        
        //db
        $crud->where(array('clients.deleted' => 0));
        $crud->order_by('clients.status');
        
        //settings before saving
        $crud->columns('company','branches','contract_start','contract_end','contact_person','contact_no','email');
        $crud->fields('company','branches','contract_start','contract_end','contact_person','contact_no','email','status');
        $crud->callback_column('contract_start',array($this,'_grocerycrud_date_format'));
        $crud->callback_column('contract_end',array($this,'_grocerycrud_date_format'));
        $crud->callback_column('email',array($this,'_grocerycrud_mailto'));
        
        //additional action buttons
        $crud->add_action('Remove Client',site_url('html/images/soft_delete.png'),'admin_panel/client/delete','confirm-delete');
        $crud->add_action('Record Playlist',site_url('html/images/playlist.png'),'admin_panel/client/playlist');
        
        $crud->unset_fields('deleted');
        $crud->unset_delete();
        
        //settings validation before saving
        $crud->set_rules('company','Company','required');
        $crud->set_rules('branches','Branches','required|integer');
        $crud->set_rules('contract_start','Contract Start','required');
        $crud->set_rules('contract_end','Contract End','required');
        $crud->set_rules('contact_person','','');
        $crud->set_rules('email','','valid_email');
        $crud->set_rules('contact_no','','required');
        
        $output = $crud->render();
        $this->load->vars($output);
        
        $this->load->view('admin_panel/client/main');
    }
    
    public function playlist($client_id)
    {
        $mparam = array(
            'table'     =>  'clients',
            'condition' =>  array('id' => $client_id),
            'fetch'     =>  'row'
        );
        
        $data['record'] = $this->general_model->_get_record($mparam);
        
        if(!$data['record'])
        {
            redirect('admin_panel/client');
        }
        
        $this->load->vars($data);
        
        //init
        $crud = new grocery_CRUD();
        $crud->set_table('playlists');
        $crud->set_subject('Client Playlists');
        
        $crud->order_by('playlists.created');
        $crud->where('playlists.client_id',$client_id);
        
        $crud->set_relation_n_n('songs', 'playlist_songs', 'songs', 'playlist_id', 'song_id', 'title','priority');
        $crud->fields('label','songs','client_id');
        $crud->change_field_type('client_id', 'hidden', $client_id);
        
        $crud->columns('label','songs');
        $crud->display_as('songs','Song Title(s)');
        
        $crud->callback_column('songs',array($this,'_song_list'));
        
        $crud->add_action('Remove Playlist',site_url('html/images/soft_delete.png'),'admin_panel/client/playlist_delete','confirm-delete');
        
        $crud->unset_fields('deleted');
        $crud->unset_delete();
        
        $output = $crud->render();
        $this->load->vars($output);
        
        $this->load->view('admin_panel/client/playlist');
    }
    
    public function _song_list($value, $row)
    {
        return $value;
    }
    
    public function playlist_delete($id)
    {
        $param = array(
            'table'     =>  'playlists',
            'fields'    =>  array('deleted' => 1),
            'condition' =>  array('id' => $id)
        );
        if($this->general_model->_save_record($param))
        {
            $this->session->set_flashdata('success_message','Playlist Record removed.');
            redirect($_SERVER['HTTP_REFERER']);
        }
    }
    
    public function delete($client_id)
    {
        $param = array(
            'table'     =>  'clients',
            'fields'    =>  array('deleted' => 1),
            'condition' =>  array('id' => $client_id)
        );
        if($this->general_model->_save_record($param))
        {
            $this->session->set_flashdata('success_message','Client removed.');
            redirect('admin_panel/client/main');
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */